﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace PageReplace
{
    public partial class Form1 : Form
    {

        public int[] visitPages = null;//访问的页面序列
        public int visitNum = 20;//需要访问的页面数
        public int memBlockNum = 3;      //物理块数量
        private int memoryAccessTime = 50;//访问一次内存的时间
        private int FasttableAccessTime = 5;//访问快表时间
        private int MissingpagesTime = 1000; //缺页中断时间

        public int[] pageTable = null;//页表

        private double FIFOtime = 0;
        private double FIFOlackrate = 0;
        private double LRUtime = 0;
        private double LRUlackrate = 0;
        private double OPTtime = 0;
        private double OPTlackrate = 0;
        private double LFUtime = 0;
        private double LFUlackrate = 0;
        bool fast = true;
        Pen p = new Pen(Color.Black);
        Brush Blackbrush = new SolidBrush(Color.Black);
        Brush Redbrush = new SolidBrush(Color.Red);
        Mutex mutex = new Mutex();
        Thread FIFOThread = null;
        Thread LRUThread = null;
        Thread OPTThread = null;
        Thread LFUThread = null;
        Bitmap bp1;
        Bitmap bp2;
        Bitmap bp3;
        Bitmap bp4;


        int FIFOtableX = 10;
        int FIFOtableY = 10;
        int FIFOtableCurrentX = 10;
        int FIFOtableCurrentY = 10;
        int LRUtableX = 10;
        int LRUtableY = 10;
        int LRUtableCurrentX = 10;
        int LRUtableCurrentY = 10;
        int OPTtableX = 10;
        int OPTtableY = 10;
        int OPTtableCurrentX = 10;
        int OPTtableCurrentY = 10;

        int LFUtableX = 10;
        int LFUtableY = 10;
        int LFUtableCurrentX = 10;
        int LFUtableCurrentY = 10;

        int tablewidth = 38;
        int tableheight = 15;
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            btnFIFO.Enabled = true;
            btnStopFIFO.Enabled = false;
            btnLRU.Enabled = true;
            btnStopLRU.Enabled = false;
            btnOPT.Enabled = true;
            btnStopOPT.Enabled = false;

            btnLRU.Enabled = true;
            btnStopLFU.Enabled = false;

            btnALL.Enabled = true;
            btnStopALL.Enabled = false;
            btnsave.Enabled = true;
            int num = int.Parse(memblockTextbox.Text);
            pageTable = new int[num];
            Random rd = new Random((int)DateTime.Now.Ticks);
            Dictionary<int, int> mp = new Dictionary<int, int>();
            int temp;
            for (int i = 0; i < num;)
            {
                temp = rd.Next(10);
                if (!mp.ContainsKey(temp))
                {
                    mp.Add(temp, 1);
                    pageTable[i] = temp;
                    i++;
                }
                else
                    continue;
            }
            memblockTextbox_lostFocus(null, null);
        }



        public void FIFO()
        {
            bp1 = new Bitmap(1000, 300);
            Graphics g = Graphics.FromImage(bp1);
            g.Clear(Color.White);
            FIFOtableCurrentX = FIFOtableX;
            mutex.WaitOne();
            FIFOtableCurrentY = FIFOtableY;

            g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("访问页面", new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 2, FIFOtableCurrentY + 2);
            for (int i = 0; i < memBlockNum; i++)
            {
                FIFOtableCurrentY += tableheight;
                g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth * 2, tableheight);
                g.DrawString("物理块" + (i + 1), new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 2, FIFOtableCurrentY + 2);
            }
            FIFOtableCurrentY += tableheight;
            g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("是否缺页", new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 2, FIFOtableCurrentY + 2);
            FIFOtableCurrentY += tableheight;
            g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("缺页数", new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 2, FIFOtableCurrentY + 2);

            FIFOtableCurrentY += tableheight;

            mutex.ReleaseMutex();
            FIFOtableCurrentX += tablewidth * 2;
            int totalSleep = 0;
            Queue<int> q = new Queue<int>();

            int[] memBlock = new int[memBlockNum];
            for (int i = 0; i < memBlockNum; i++)
                memBlock[i] = -1;
            int missingPageNum = 0;
            int sleepTime = 0;
            int num = 0;
            for (int i = 0; i < visitNum; i++)
            {
                sleepTime = 0;
                FIFOtableCurrentY = FIFOtableY;

                bool flag = false;
                for (int j = 0; j < memBlockNum; j++)
                {
                    if (visitPages[i] == memBlock[j])
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    if (missingPageNum < memBlockNum)
                    {
                        memBlock[num] = visitPages[i];

                        num++;
                        q.Enqueue(visitPages[i]);
                        missingPageNum++;

                        mutex.WaitOne();
                        g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                        g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);

                        for (int j = 0; j < memBlockNum; j++)
                        {
                            FIFOtableCurrentY += tableheight;
                            if (j < missingPageNum)
                            {

                                g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);
                            }
                            else
                            {
                                g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                            }
                        }

                        FIFOtableCurrentY += tableheight;
                        g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                        g.DrawString("√", new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);

                        FIFOtableCurrentY += tableheight;
                        g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                        g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);
                        FIFOtableCurrentY += tableheight;
                        FIFOtableCurrentX += tablewidth;
                        mutex.ReleaseMutex();
                        if (fast)
                            sleepTime = FasttableAccessTime + memoryAccessTime + MissingpagesTime + FasttableAccessTime
                                + memoryAccessTime;
                        else
                            sleepTime = memoryAccessTime * 3 + MissingpagesTime;
                    }
                    else
                    {
                        int page = q.Dequeue();
                        q.Enqueue(visitPages[i]);
                        for (int j = 0; j < memBlockNum; j++)
                        {
                            if (page == memBlock[j])
                            {
                                memBlock[j] = visitPages[i];
                                break;
                            }
                        }
                        missingPageNum++;
                        mutex.WaitOne();
                        g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                        g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);


                        for (int j = 0; j < memBlockNum; j++)
                        {
                            FIFOtableCurrentY += tableheight;
                            g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                            if (memBlock[j] == visitPages[i])
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Redbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);
                            else
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);
                        }
                        FIFOtableCurrentY += tableheight;
                        g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                        g.DrawString("√", new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 12, FIFOtableCurrentY + 2);

                        FIFOtableCurrentY += tableheight;
                        g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                        g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);
                        FIFOtableCurrentY += tableheight;
                        FIFOtableCurrentX += tablewidth;
                        mutex.ReleaseMutex();
                        if (fast)
                            sleepTime = FasttableAccessTime + memoryAccessTime + MissingpagesTime + FasttableAccessTime
                                + memoryAccessTime;
                        else
                            sleepTime = memoryAccessTime * 3 + MissingpagesTime;
                    }
                }
                else
                {

                    mutex.WaitOne();
                    g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);
                    g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);
                    for (int j = 0; j < memBlockNum + 2; j++)
                    {
                        FIFOtableCurrentY += tableheight;
                        g.DrawRectangle(p, FIFOtableCurrentX, FIFOtableCurrentY, tablewidth, tableheight);

                    }
                    g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, FIFOtableCurrentX + 15, FIFOtableCurrentY + 2);
                    FIFOtableCurrentY += tableheight;
                    mutex.ReleaseMutex();
                    FIFOtableCurrentX += tablewidth;
                    if (fast)
                        sleepTime = FasttableAccessTime + memoryAccessTime;
                    else
                        sleepTime = memoryAccessTime * 2;
                }

                totalSleep += sleepTime;
                this.BeginInvoke(new Action(() =>
                {

                    FIFOLacklabel.Text = "FIFO当前缺页率：" + missingPageNum + "/" + (i + 1) + "=" + ((missingPageNum * 1.0 / (i + 1)) * 100).ToString("F2") + "%";
                }));

                this.BeginInvoke(new Action(() =>
                {

                    lableFIFOsleep.Text = "FIFO当前累计用时：" + totalSleep + "ms";
                }));
                g.Save();
                pictureBoxFIFO.Image = bp1;
                Thread.Sleep(sleepTime);

            }
            mutex.WaitOne();
            FIFOtableCurrentY = FIFOtableY;
            g.DrawString("缺页率为" + ((double)missingPageNum / visitNum * 100) + "%", new Font("黑体", 12), Blackbrush, FIFOtableCurrentX + 2, FIFOtableCurrentY + 2);
            FIFOtableCurrentY += tableheight;
            g.DrawString("总时间为：" + totalSleep + "ms", new Font("黑体", 12), Blackbrush, FIFOtableCurrentX + 2, FIFOtableCurrentY + 2);
            g.Save();
            pictureBoxFIFO.Image = bp1;
            mutex.ReleaseMutex();
            FIFOtime = totalSleep;
            FIFOlackrate = (double)missingPageNum / visitNum * 100;
            MessageBox.Show("FIFO算法结束", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void LRU()
        {
            bp2 = new Bitmap(1000, 300);
            Graphics g = Graphics.FromImage(bp2);
            g.Clear(Color.White);
            LRUtableCurrentX = LRUtableX;

            mutex.WaitOne();
            LRUtableCurrentY = LRUtableY;
            g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("访问页面", new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 2, LRUtableCurrentY + 2);
            for (int i = 0; i < memBlockNum; i++)
            {
                LRUtableCurrentY += tableheight;
                g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth * 2, tableheight);
                g.DrawString("物理块" + (i + 1), new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 2, LRUtableCurrentY + 2);
            }
            LRUtableCurrentY += tableheight;
            g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("是否缺页", new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 2, LRUtableCurrentY + 2);
            LRUtableCurrentY += tableheight;
            g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("缺页数", new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 2, LRUtableCurrentY + 2);

            LRUtableCurrentY += tableheight;
            mutex.ReleaseMutex();
            LRUtableCurrentX += tablewidth * 2;
            int totalSleep = 0;
            int sleepTime = 0;
            int num = 0;
            Dictionary<int, int> map = new Dictionary<int, int>();

            int[] memBlock = new int[memBlockNum];
            for (int i = 0; i < memBlockNum; i++)
                memBlock[i] = -1;
            int missingPageNum = 0;
            for (int i = 0; i < visitNum; i++)
            {

                LRUtableCurrentY = LRUtableY;
                bool flag = false;
                for (int j = 0; j < memBlockNum; j++)
                {
                    if (visitPages[i] == memBlock[j])
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    if (missingPageNum < memBlockNum)
                    {

                        memBlock[num] = visitPages[i];

                        num++;

                        missingPageNum++;
                        map.Add(visitPages[i], i);

                        mutex.WaitOne();

                        g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                        g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 15, LRUtableCurrentY + 2);

                        for (int j = 0; j < memBlockNum; j++)
                        {
                            LRUtableCurrentY += tableheight;
                            if (j < missingPageNum)
                            {

                                g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 15, LRUtableCurrentY + 2);
                            }
                            else
                            {
                                g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                            }
                        }

                        LRUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                        g.DrawString("√", new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 12, LRUtableCurrentY + 2);


                        LRUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                        g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 15, LRUtableCurrentY + 2);
                        LRUtableCurrentY += tableheight;
                        LRUtableCurrentX += tablewidth;
                        mutex.ReleaseMutex();
                        if (fast)
                            sleepTime = FasttableAccessTime + memoryAccessTime + MissingpagesTime + FasttableAccessTime
                                + memoryAccessTime;
                        else
                            sleepTime = memoryAccessTime * 3 + MissingpagesTime;
                    }
                    else
                    {
                        int mn = 999;
                        int page = -1;

                        foreach (var item in map)
                        {
                            for (int j = 0; j < memBlockNum; j++)
                                if (mn > item.Value && item.Key == memBlock[j])
                                {
                                    mn = item.Value;
                                    page = item.Key;
                                }
                        }
                        for (int j = 0; j < memBlockNum; j++)
                        {
                            if (page == memBlock[j])
                            {
                                memBlock[j] = visitPages[i];

                                break;
                            }
                        }
                        missingPageNum++;
                        if (map.ContainsKey(visitPages[i]))
                        {
                            map[visitPages[i]] = i;
                        }
                        else
                        {
                            map.Add(visitPages[i], i);
                        }
                        mutex.WaitOne();
                        g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                        g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 15, LRUtableCurrentY + 2);


                        for (int j = 0; j < memBlockNum; j++)
                        {
                            LRUtableCurrentY += tableheight;
                            g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                            if (memBlock[j] == visitPages[i])
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Redbrush, LRUtableCurrentX + 15, LRUtableCurrentY + 2);
                            else
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 15, LRUtableCurrentY + 2);
                        }
                        LRUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                        g.DrawString("√", new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 12, LRUtableCurrentY + 2);


                        LRUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                        g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 15, LRUtableCurrentY + 2);
                        LRUtableCurrentY += tableheight;
                        LRUtableCurrentX += tablewidth;
                        mutex.ReleaseMutex();
                        if (fast)
                            sleepTime = FasttableAccessTime + memoryAccessTime + MissingpagesTime + FasttableAccessTime
                                + memoryAccessTime;
                        else
                            sleepTime = memoryAccessTime * 3 + MissingpagesTime;
                    }
                }
                else
                {
                    if (map.ContainsKey(visitPages[i]))
                        map[visitPages[i]] = i;

                    mutex.WaitOne();
                    g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);
                    g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 15, LRUtableCurrentY + 2);
                    for (int j = 0; j < memBlockNum + 2; j++)
                    {
                        LRUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LRUtableCurrentX, LRUtableCurrentY, tablewidth, tableheight);

                    }
                    g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, LRUtableCurrentX + 15, LRUtableCurrentY + 2);
                    LRUtableCurrentY += tableheight;
                    mutex.ReleaseMutex();
                    LRUtableCurrentX += tablewidth;
                    if (fast)
                        sleepTime = FasttableAccessTime + memoryAccessTime;
                    else
                        sleepTime = memoryAccessTime * 2;
                }

                totalSleep += sleepTime;
                this.BeginInvoke(new Action(() =>
                {

                    LRULacklabel.Text = "LRU当前缺页率：" + missingPageNum + "/" + (i + 1) + "=" + ((missingPageNum * 1.0 / (i + 1)) * 100).ToString("F2") + "%";
                }));

                this.BeginInvoke(new Action(() =>
                {

                    lableLRUsleep.Text = "LRU当前累计用时：" + totalSleep + "ms";
                }));
                g.Save();
                pictureBoxLRU.Image = bp2;
                Thread.Sleep(sleepTime);
            }
            mutex.WaitOne();
            LRUtableCurrentY = LRUtableY;
            g.DrawString("缺页率为" + ((double)missingPageNum / visitNum * 100) + "%", new Font("黑体", 12), Blackbrush, LRUtableCurrentX + 2, LRUtableCurrentY + 2);
            LRUtableCurrentY += tableheight;
            g.DrawString("总时间为：" + totalSleep + "ms", new Font("黑体", 12), Blackbrush, LRUtableCurrentX + 2, LRUtableCurrentY + 2);
            g.Save();
            pictureBoxLRU.Image = bp2;
            mutex.ReleaseMutex();
            LRUtime = totalSleep;
            LRUlackrate = (double)missingPageNum / visitNum * 100;
            MessageBox.Show("LRU算法结束", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public void OPT()
        {
            bp3 = new Bitmap(1000, 300);
            Graphics g = Graphics.FromImage(bp3);
            g.Clear(Color.White);
            OPTtableCurrentX = OPTtableX;
            mutex.WaitOne();
            OPTtableCurrentY = OPTtableY;

            g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("访问页面", new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 2, OPTtableCurrentY + 2);
            for (int i = 0; i < memBlockNum; i++)
            {
                OPTtableCurrentY += tableheight;
                g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth * 2, tableheight);
                g.DrawString("物理块" + (i + 1), new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 2, OPTtableCurrentY + 2);
            }
            OPTtableCurrentY += tableheight;
            g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("是否缺页", new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 2, OPTtableCurrentY + 2);
            OPTtableCurrentY += tableheight;
            g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("缺页数", new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 2, OPTtableCurrentY + 2);
            OPTtableCurrentY += tableheight;
            mutex.ReleaseMutex();
            OPTtableCurrentX += tablewidth * 2;
            int totalSleep = 0;
            int sleepTime = 0;
            int num = 0;
            Dictionary<int, int> map = new Dictionary<int, int>();
            int[] memBlock = new int[memBlockNum];
            for (int i = 0; i < memBlockNum; i++)
                memBlock[i] = -1;
            int missingPageNum = 0;


            for (int i = 0; i < visitNum; i++)
            {
                OPTtableCurrentY = OPTtableY;

                bool flag = false;
                for (int j = 0; j < memBlockNum; j++)
                {
                    if (visitPages[i] == memBlock[j])
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {//缺页
                    if (missingPageNum < memBlockNum)
                    {//物理块未满
                        memBlock[num] = visitPages[i];

                        num++;
                        missingPageNum++;
                        mutex.WaitOne();

                        g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                        g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 15, OPTtableCurrentY + 2);

                        for (int j = 0; j < memBlockNum; j++)
                        {
                            OPTtableCurrentY += tableheight;
                            if (j < missingPageNum)
                            {

                                g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 15, OPTtableCurrentY + 2);
                            }
                            else
                            {
                                g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                            }
                        }

                        OPTtableCurrentY += tableheight;
                        g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                        g.DrawString("√", new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 12, OPTtableCurrentY + 2);


                        OPTtableCurrentY += tableheight;
                        g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                        g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 15, OPTtableCurrentY + 2);
                        OPTtableCurrentY += tableheight;
                        OPTtableCurrentX += tablewidth;
                        mutex.ReleaseMutex();

                        if (fast)
                            sleepTime = FasttableAccessTime + memoryAccessTime + MissingpagesTime + FasttableAccessTime
                                + memoryAccessTime;
                        else
                            sleepTime = memoryAccessTime * 3 + MissingpagesTime;

                    }
                    else
                    {//物理块满
                        map.Clear();
                        int mm = -1;
                        for (int z = 0; z < memBlockNum; z++)
                            for (int j = i; j < visitNum; j++)
                            {
                                if (memBlock[z] == visitPages[j])
                                {
                                    map.Add(visitPages[j], j);
                                    break;
                                    //将页面在之后出现的时间或者位置记录在value中
                                }
                            }
                        int page = -1;
                        foreach (var item in map)
                        {
                            for (int j = 0; j < memBlockNum; j++)
                                if (mm < item.Value && item.Key == memBlock[j])
                                {
                                    mm = item.Value;
                                    page = item.Key;
                                    //找到将来最久未使用的页面page
                                }
                        }
                        for (int j = 0; j < memBlockNum; j++)
                        {
                            if (!map.ContainsKey(memBlock[j]))
                            {//物理块中的页面不在map中表示之后的序列没有出现该页面
                                page = memBlock[j];
                                break;
                            }
                        }

                        for (int j = 0; j < memBlockNum; j++)
                        {
                            if (page == memBlock[j])
                            {
                                memBlock[j] = visitPages[i];

                                break;
                            }
                        }
                        missingPageNum++;

                        mutex.WaitOne();
                        g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                        g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 15, OPTtableCurrentY + 2);


                        for (int j = 0; j < memBlockNum; j++)
                        {
                            OPTtableCurrentY += tableheight;
                            g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                            if (memBlock[j] == visitPages[i])
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Redbrush, OPTtableCurrentX + 15, OPTtableCurrentY + 2);
                            else
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 15, OPTtableCurrentY + 2);
                        }
                        OPTtableCurrentY += tableheight;
                        g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                        g.DrawString("√", new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 12, OPTtableCurrentY + 2);


                        OPTtableCurrentY += tableheight;
                        g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                        g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 15, OPTtableCurrentY + 2);
                        OPTtableCurrentY += tableheight;
                        OPTtableCurrentX += tablewidth;
                        mutex.ReleaseMutex();
                        if (fast)
                            sleepTime = FasttableAccessTime + memoryAccessTime + MissingpagesTime + FasttableAccessTime
                                + memoryAccessTime;
                        else
                            sleepTime = memoryAccessTime * 3 + MissingpagesTime;
                    }
                }
                else
                {

                    mutex.WaitOne();
                    g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);
                    g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 15, OPTtableCurrentY + 2);
                    for (int j = 0; j < memBlockNum + 2; j++)
                    {
                        OPTtableCurrentY += tableheight;
                        g.DrawRectangle(p, OPTtableCurrentX, OPTtableCurrentY, tablewidth, tableheight);

                    }
                    g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, OPTtableCurrentX + 15, OPTtableCurrentY + 2);
                    OPTtableCurrentY += tableheight;
                    mutex.ReleaseMutex();
                    OPTtableCurrentX += tablewidth;
                    if (fast)
                        sleepTime = FasttableAccessTime + memoryAccessTime;
                    else
                        sleepTime = memoryAccessTime * 2;
                }


                totalSleep += sleepTime;
                this.BeginInvoke(new Action(() =>
                {

                    OPTLacklabel.Text = "OPT当前缺页率：" + missingPageNum + "/" + (i + 1) + "=" + ((missingPageNum * 1.0 / (i + 1)) * 100).ToString("F2") + "%";
                }));

                this.BeginInvoke(new Action(() =>
                {

                    lableOPTsleep.Text = "OPT当前累计用时：" + totalSleep + "ms";
                }));
                g.Save();
                pictureBoxOPT.Image = bp3;
                Thread.Sleep(sleepTime);
            }
            mutex.WaitOne();
            OPTtableCurrentY = OPTtableY;
            g.DrawString("缺页率为" + ((double)missingPageNum / visitNum * 100) + "%", new Font("黑体", 12), Blackbrush, OPTtableCurrentX + 2, OPTtableCurrentY + 2);
            OPTtableCurrentY += tableheight;
            g.DrawString("总时间为：" + totalSleep + "ns", new Font("黑体", 12), Blackbrush, OPTtableCurrentX + 2, OPTtableCurrentY + 2);
            g.Save();
            pictureBoxOPT.Image = bp3;
            mutex.ReleaseMutex();
            OPTtime = totalSleep;
            OPTlackrate = (double)missingPageNum / visitNum * 100;
            MessageBox.Show("OPT算法结束", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public void LFU()
        {
            bp4 = new Bitmap(1000, 300);
            Graphics g = Graphics.FromImage(bp4);
            g.Clear(Color.White);

            LFUtableCurrentX = LFUtableX;

            mutex.WaitOne();

            LFUtableCurrentY = LFUtableY;

            g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth * 2, tableheight * 2);
            g.DrawString("访问页面", new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 2, LFUtableCurrentY + 2);

            for (int i = 0; i < memBlockNum; i++)
            {
                LFUtableCurrentY += tableheight;
                g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth * 2, tableheight);
                g.DrawString("物理块" + (i + 1), new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 2, LFUtableCurrentY + 2);
            }
            LFUtableCurrentY += tableheight;
            g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("是否缺页", new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 2, LFUtableCurrentY + 2);

            LFUtableCurrentY += tableheight;
            g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth * 2, tableheight);
            g.DrawString("缺页数", new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 2, LFUtableCurrentY + 2);

            LFUtableCurrentY += tableheight;

            mutex.ReleaseMutex();

            LFUtableCurrentX += tablewidth * 2;

            int totalSleep = 0;
            int sleepTime = 0;
            int num = 0;
            Dictionary<int, int> map = new Dictionary<int, int>();

            int[] memBlock = new int[memBlockNum];
            for (int i = 0; i < memBlockNum; i++)
                memBlock[i] = -1;
            int missingPageNum = 0;

            for (int i = 0; i < visitNum; i++)
            {
                LFUtableCurrentY = LFUtableY;
                bool flag = false;

                for (int j = 0; j < memBlockNum; j++)
                {
                    if (visitPages[i] == memBlock[j])
                    {
                        flag = true;
                        break;//物理块中存在当前页面
                    }
                    else continue;
                }
                if (!flag)//缺页
                {
                    if (missingPageNum < memBlockNum)//缺页数小于物理块数
                    {
                        memBlock[num] = visitPages[i];

                        num++;
                        missingPageNum++;
                        map.Add(visitPages[i], 1);

                        Console.WriteLine(map[visitPages[i]]);

                        mutex.WaitOne();
                        g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                        g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 15, LFUtableCurrentY + 2);

                        for (int j = 0; j < memBlockNum; j++)
                        {

                            LFUtableCurrentY += tableheight;
                            g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                            if (j < missingPageNum)
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 15, LFUtableCurrentY + 2);
                        }
                        LFUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                        g.DrawString("√", new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 12, LFUtableCurrentY + 2);

                        LFUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                        g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 15, LFUtableCurrentY + 2);

                        LFUtableCurrentY += tableheight;
                        LFUtableCurrentX += tablewidth;
                        mutex.ReleaseMutex();

                        // 计算睡眠时间
                        if (fast)
                            sleepTime = FasttableAccessTime + memoryAccessTime + MissingpagesTime + FasttableAccessTime + memoryAccessTime;
                        else
                            sleepTime = memoryAccessTime * 3 + MissingpagesTime;
                    }
                    else
                    {//物理块满，需要替换页面
                        int mn = 999;
                        int page = -1;
                        foreach (var item in map)
                        {//Key存储页面号，Value存储使用次数
                            for (int j = 0; j < memBlockNum; j++)
                                if (mn > item.Value && item.Key == memBlock[j])//在物理块中且访问次数最少
                                {
                                    mn = item.Value;
                                    page = item.Key;
                                    //mn表示使用次数 page 表示该页面号
                                }
                        }
                        for (int j = 0; j < memBlockNum; j++)
                        {
                            if (page == memBlock[j])
                            {
                                memBlock[j] = visitPages[i];
                                break;
                            }
                        }
                        missingPageNum++;
                        if (map.ContainsKey(visitPages[i]))
                            map[visitPages[i]] = map[visitPages[i]] + 1;
                        else
                            map.Add(visitPages[i], 1);

                        // 进入互斥锁，绘制页面替换信息
                        mutex.WaitOne();
                        g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                        g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 15, LFUtableCurrentY + 2);

                        for (int j = 0; j < memBlockNum; j++)
                        {
                            LFUtableCurrentY += tableheight;
                            g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                            if (memBlock[j] == visitPages[i])
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Redbrush, LFUtableCurrentX + 15, LFUtableCurrentY + 2);
                            else
                                g.DrawString(memBlock[j].ToString(), new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 15, LFUtableCurrentY + 2);
                        }

                        LFUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                        g.DrawString("√", new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 12, LFUtableCurrentY + 2);

                        LFUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                        g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 15, LFUtableCurrentY + 2);

                        LFUtableCurrentY += tableheight;
                        LFUtableCurrentX += tablewidth;
                        mutex.ReleaseMutex();

                        // 计算睡眠时间
                        if (fast)
                            sleepTime = FasttableAccessTime + memoryAccessTime + MissingpagesTime + FasttableAccessTime + memoryAccessTime;
                        else
                            sleepTime = memoryAccessTime * 3 + MissingpagesTime;
                    }
                }
                else
                {//当前页面在物理块中，更新其使用次数
                    if (map.ContainsKey(visitPages[i]))
                        map[visitPages[i]] = map[visitPages[i]] + 1;


                    // 进入互斥锁，绘制页面命中信息
                    mutex.WaitOne();
                    g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                    g.DrawString(visitPages[i].ToString(), new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 15, LFUtableCurrentY + 2);

                    for (int j = 0; j < memBlockNum + 2; j++)
                    {
                        LFUtableCurrentY += tableheight;
                        g.DrawRectangle(p, LFUtableCurrentX, LFUtableCurrentY, tablewidth, tableheight);
                    }

                    g.DrawString(missingPageNum.ToString(), new Font("黑体", 9), Blackbrush, LFUtableCurrentX + 15, LFUtableCurrentY + 2);
                    LFUtableCurrentY += tableheight;
                    mutex.ReleaseMutex();

                    LFUtableCurrentX += tablewidth;
                    if (fast)
                        sleepTime = FasttableAccessTime + memoryAccessTime;
                    else
                        sleepTime = memoryAccessTime * 2;
                }
                // 更新总等待时间
                totalSleep += sleepTime;

                // 使用Invoke更新UI
                this.BeginInvoke(new Action(() =>
                {
                    LFULacklabel.Text = "LFU当前缺页率：" + missingPageNum + "/" + (i + 1) + "=" + ((missingPageNum * 1.0 / (i + 1)) * 100).ToString("F2") + "%";
                }));

                this.BeginInvoke(new Action(() =>
                {
                    lableLFUsleep.Text = "LFU当前累计用时：" + totalSleep + "ms";
                }));

                g.Save();
                pictureBoxLFU.Image = bp4;
                Thread.Sleep(sleepTime);  // 等待一定时间

            }
            mutex.WaitOne();
            LFUtableCurrentY = LFUtableY;
            g.DrawString("缺页率为" + ((double)missingPageNum / visitNum * 100) + "%", new Font("黑体", 12), Blackbrush, LFUtableCurrentX + 2, LFUtableCurrentY + 2);
            LFUtableCurrentY += tableheight;
            g.DrawString("总时间为：" + totalSleep + "ms", new Font("黑体", 12), Blackbrush, LFUtableCurrentX + 2, LFUtableCurrentY + 2);
            g.Save();
            pictureBoxLFU.Image = bp4;
            mutex.ReleaseMutex();
            LFUtime = totalSleep;
            LFUlackrate = (double)missingPageNum / visitNum * 100;
            MessageBox.Show("LFU算法结束", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);



        }


        private void Form1_Load(object sender, EventArgs e)
        {
            //memblockTextbox.LostFocus += new EventHandler(memblockTextbox_lostFocus);
            pagesTextbox1.LostFocus += new EventHandler(AddressTextBox_lostFocus);
            //neicun_time.LostFocus += new EventHandler(neicun_timeLostFocus);
            btn_initialize.Click += new EventHandler(image_null);
            btn_initialize.Click += new EventHandler(btnRandom_Click);
            btn_initialize.Click += new EventHandler(btnPage_Click);

        }
        private void neicun_timeLostFocus(object sender, EventArgs e)
        {
            if (neicun_time.Text == String.Empty)
            {
                MessageBox.Show("内存访问时间不可为空!!");
                neicun_time.Text = "50";
            }
            else
                memoryAccessTime = int.Parse(neicun_time.Text);
        }
        private void qyzd_timeLosstFoucs(object sender, EventArgs e)
        {
            if (qyzd_time.Text == String.Empty)
            {
                MessageBox.Show("缺页中断时间不可为空!!");
                qyzd_time.Text = "1000";
            }
            else MissingpagesTime = int.Parse(qyzd_time.Text);
        }
        private void kb_timeLostFocus(object sender, EventArgs e)
        {
            if (kb_time.Text == String.Empty)
            {
                MessageBox.Show("快表访问时间不可为空!!");
                kb_time.Text = "5";
            }
            else FasttableAccessTime = int.Parse(kb_time.Text);
        }
        public void AddressTextBox_lostFocus(object sender, EventArgs e)
        {
            string[] temp = pagesTextbox1.Text.Split(' ');
            int num = int.Parse(pageNumTextbox.Text);

            if (temp.Length != num)
            {
                MessageBox.Show("输入地址序列数量不对");
                pagesTextbox1.Text = "";
            }


        }
        public void memblockTextbox_lostFocus(object sender, EventArgs e)
        {
            int num;
            if (memblockTextbox.Text == String.Empty)
            {
                num = 0;
                MessageBox.Show("物理块数不可为空！！");
                memblockTextbox.Text = "3";
            }
            else
            {
                num = int.Parse(memblockTextbox.Text);
                if (num < 0 || num > 10)
                {
                    MessageBox.Show("输入数据有误！");
                    memblockTextbox.Text = "3";
                    num = 3;
                }
                else memBlockNum = num;
            }
            pageTable = new int[num];
            Random rd = new Random((int)DateTime.Now.Ticks);
            Dictionary<int, int> mp = new Dictionary<int, int>();
            int temp;
            for (int i = 0; i < num;)
            {
                temp = rd.Next(10);
                if (!mp.ContainsKey(temp))
                {
                    mp.Add(temp, 1);
                    pageTable[i] = temp;
                    i++;
                }
                else
                    continue;
            }
            Bitmap bp = new Bitmap(1000, 1000);
            Graphics g = Graphics.FromImage(bp);
            g.Clear(Color.White);

            Pen p = new Pen(Color.Black);

            Brush Blackbrush = new SolidBrush(Color.Black);

            int tableX = 0;
            int tableY = 0;
            int currentX;
            int currentY;
            int width = 55;
            int height = 20;
            Mutex mutex = new Mutex();
            currentY = tableY;
            mutex.WaitOne();
            currentX = tableX;
            g.DrawRectangle(p, currentX, currentY, width, height);
            g.DrawString("页号", new Font("黑体", 10), Blackbrush, currentX, currentY + 2);
            currentX += width;
            g.DrawRectangle(p, currentX, currentY, width, height);
            g.DrawString("内存块号", new Font("黑体", 10), Blackbrush, currentX, currentY + 2);
            currentY += height;
            mutex.ReleaseMutex();
            for (int i = 0; i < pageTable.Length; i++)
            {
                mutex.WaitOne();
                currentX = tableX;
                g.DrawRectangle(p, currentX, currentY, width, height);
                g.DrawString(i.ToString(), new Font("黑体", 10), Blackbrush, currentX + 20, currentY + 2);
                currentX += width;
                g.DrawRectangle(p, currentX, currentY, width, height);
                g.DrawString(pageTable[i].ToString(), new Font("黑体", 10), Blackbrush, currentX + 20, currentY + 2);
                currentY += height;
                mutex.ReleaseMutex();
            }
            g.Save();
            pictureBoxPagetable.Image = bp;
        }
        private void btnFIFO_Click(object sender, EventArgs e)
        {
           btnPage_Click(null, null);
            if (useFast.Checked)
                fast = true;
            else if (noFast.Checked)
                fast = false;
            FIFOThread = new Thread(new ThreadStart(FIFO));
            FIFOThread.IsBackground = true;
            FIFOThread.Start();
            btnFIFO.Enabled = false;
            btnStopFIFO.Enabled = true;
        }
        private void btnStopFIFO_Click(object sender, EventArgs e)
        {

            if (btnStopFIFO.Text == "挂起")
            {
                if (FIFOThread.IsAlive)
                {
                    FIFOThread.Suspend();
                    btnStopFIFO.Text = "激活";
                }
            }
            else if (btnStopFIFO.Text == "激活")
            {
                FIFOThread.Resume();
                btnStopFIFO.Text = "挂起";
            }
        }
        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
        private void btnLRU_Click(object sender, EventArgs e)
        {
            btnPage_Click(null, null);
            if (useFast.Checked)
                fast = true;
            else if (noFast.Checked)
                fast = false;
            LRUThread = new Thread(new ThreadStart(LRU));
            LRUThread.IsBackground = true;
            LRUThread.Start();
            btnLRU.Enabled = false;
            btnStopLRU.Enabled = true;
        }

        private void btnOPT_Click(object sender, EventArgs e)
        {
            btnPage_Click(null, null);
            if (useFast.Checked)
                fast = true;
            else if (noFast.Checked)
                fast = false;
            OPTThread = new Thread(new ThreadStart(OPT));
            OPTThread.IsBackground = true;
            OPTThread.Start();
            btnOPT.Enabled = false;
            btnStopOPT.Enabled = true;
        }
        private void btnStopLRU_Click(object sender, EventArgs e)
        {
            if (btnStopLRU.Text == "挂起")
            {
                if (LRUThread.IsAlive)
                {
                    LRUThread.Suspend();
                    btnStopLRU.Text = "激活";
                }
            }
            else if (btnStopLRU.Text == "激活")
            {
                LRUThread.Resume();
                btnStopLRU.Text = "挂起";
            }
        }
        private void btuLFU_Click(object sender, EventArgs e)
        {
            btnPage_Click(null, null);
            if (useFast.Checked)
                fast = true;
            else if (noFast.Checked)
                fast = false;
            LFUThread = new Thread(new ThreadStart(LFU));
            LFUThread.IsBackground = true;
            LFUThread.Start();
            btnLFU.Enabled = false;
            btnStopLFU.Enabled = true;
        }
        private void btnStopLFU_Click(object sender, EventArgs e)
        {
            if (btnStopLRU.Text == "挂起")
            {
                if (LFUThread.IsAlive)
                {
                    LFUThread.Suspend();
                    btnStopLRU.Text = "激活";
                }
            }
            else if (btnStopLRU.Text == "激活")
            {
                LFUThread.Resume();
                btnStopLRU.Text = "挂起";
            }
        }
        private void btnALL_Click(object sender, EventArgs e)
        {
            btnPage_Click(null, null);
            if (useFast.Checked)
                fast = true;
            else if (noFast.Checked)
                fast = false;
            FIFOThread = new Thread(new ThreadStart(FIFO));
            FIFOThread.IsBackground = true;
            FIFOThread.Start();

            LRUThread = new Thread(new ThreadStart(LRU));
            LRUThread.IsBackground = true;
            LRUThread.Start();

            OPTThread = new Thread(new ThreadStart(OPT));
            OPTThread.IsBackground = true;
            OPTThread.Start();

            LFUThread = new Thread(new ThreadStart(LFU));
            LFUThread.IsBackground = true;
            LFUThread.Start();

            btnALL.Enabled = false;
            btnStopALL.Enabled = true;
        }
        private void btnStopALL_Click(object sender, EventArgs e)
        {
            if (btnStopALL.Text == "挂起")
            {
                if (FIFOThread.IsAlive)
                    FIFOThread.Suspend();
                if (LRUThread.IsAlive)
                    LRUThread.Suspend();
                if (OPTThread.IsAlive)
                    OPTThread.Suspend();
                btnStopALL.Text = "激活";
            }
            else if (btnStopALL.Text == "激活")
            {
                FIFOThread.Resume();
                LRUThread.Resume();
                OPTThread.Resume();
                btnStopALL.Text = "挂起";
            }
        }

        private void btnRandom_Click(object sender, EventArgs e)//随机生成页面序列
        {
            btnFIFO.Enabled = true;
            btnStopFIFO.Enabled = false;
            btnLRU.Enabled = true;
            btnStopLRU.Enabled = false;
            btnOPT.Enabled = true;
            btnStopOPT.Enabled = false;

            btnLFU.Enabled = true;
            btnStopLFU.Enabled = false;

            btnALL.Enabled = true;
            btnStopALL.Enabled = false;
            int num = int.Parse(pageNumTextbox.Text);
            string result = "";
            for (int i = 0; i < num;)
            {
                Random rd = new Random((int)DateTime.Now.Ticks);
                Thread.Sleep(1);//线程挂起1ms，指示应挂起此线程以使其他等待线程能够执行
                char temp = '0';
                temp = (char)(rd.Next(10) + '0');//产生随机数字
                result += temp + " ";
                i++;
            }
            result = result.TrimEnd();
            pagesTextbox1.Text = result;
            btnsave.Enabled = true;

        }
        private void btnPage_Click(object sender, EventArgs e)
        {
            FIFOLacklabel.Text = "FIFO当前缺页率";
            LRULacklabel.Text = "LRU当前缺页率";
            OPTLacklabel.Text = "OPT当前缺页率";
            lableFIFOsleep.Text = "FIFO当前累计用时";
            lableLRUsleep.Text = "LRU当前累计用时";
            lableOPTsleep.Text = "OPT当前累计用时";
            memBlockNum = int.Parse(memblockTextbox.Text);
            visitNum = int.Parse(pageNumTextbox.Text);
            visitPages = new int[visitNum];
            string[] temp = pagesTextbox1.Text.Split(' ');
            for (int i = 0; i < temp.Length; i++)
            {
                visitPages[i] = Convert.ToInt32(temp[i]);
            }


        }
        private void btnStopOPT_Click(object sender, EventArgs e)
        {
            if (btnStopOPT.Text == "挂起")
            {
                if (OPTThread.IsAlive)
                {
                    OPTThread.Suspend();
                    btnStopOPT.Text = "激活";
                }
            }
            else if (btnStopOPT.Text == "激活")
            {
                OPTThread.Resume();
                btnStopOPT.Text = "挂起";
            }
        }

        private void button1_Click(object sender, EventArgs e)//分析数据按钮
        {

            DataTable dt1 = default(DataTable);
            dt1 = CreateDataTable1();
            chart1.DataSource = dt1;
            chart1.Series[0].YValueMembers = "vo1";
            chart1.Series[0].XValueMember = "algorithm";
            chart1.DataBind();

            DataTable dt2 = default(DataTable);
            dt2 = CreateDataTable2();
            chart2.DataSource = dt2;
            chart2.Series[0].YValueMembers = "vo1";
            chart2.Series[0].XValueMember = "algorithm";
            chart2.DataBind();

        }
        private DataTable CreateDataTable1()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("algorithm");
            dt.Columns.Add("vo1");
            DataRow dr;
            dr = dt.NewRow();
            dr["algorithm"] = "FIFO";
            dr["vo1"] = FIFOlackrate;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["algorithm"] = "LRU";
            dr["vo1"] = LRUlackrate;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["algorithm"] = "OPT";
            dr["vo1"] = OPTlackrate;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["algorithm"] = "LFU";
            dr["vo1"] = LFUlackrate;
            dt.Rows.Add(dr);
            return dt;
        }
        private DataTable CreateDataTable2()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("algorithm");
            dt.Columns.Add("vo1");
            DataRow dr;
            dr = dt.NewRow();
            dr["algorithm"] = "FIFO";
            dr["vo1"] = FIFOtime;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["algorithm"] = "LRU";
            dr["vo1"] = LRUtime;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["algorithm"] = "OPT";
            dr["vo1"] = OPTtime;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["algorithm"] = "LFU";
            dr["vo1"] = LFUtime;
            dt.Rows.Add(dr);
            return dt;
        }



        private void lableOPTsleep_Click(object sender, EventArgs e)
        {

        }


        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label20_Click(object sender, EventArgs e)
        {

        }

        private void pageNumTextBoxLostFocus(object sender, EventArgs e)
        {
            int pagenum = 20;
            if (pageNumTextbox.Text == String.Empty)
            {
                MessageBox.Show("输入不得为空！");
                pageNumTextbox.Text = "20";
            }
            else
            {
                pagenum = int.Parse(pageNumTextbox.Text);
                if (pagenum <= 0 || pagenum > 50)
                {
                    MessageBox.Show("数据有误");
                    pageNumTextbox.Text = "20";
                }
            }
        }

        private void btn_initialize_Click(object sender, EventArgs e)
        {
            pageNumTextbox.Text = "20";
            memblockTextbox.Text = "3";
            neicun_time.Text = "50";
            qyzd_time.Text = "1000";
            kb_time.Text = "5";

            visitNum = 20;//需要访问的页面数
            memBlockNum = 3;      //物理块数量
            memoryAccessTime = 50;//访问一次内存的时间
            FasttableAccessTime = 5;//访问快表时间
            MissingpagesTime = 1000; //缺页中断时间

            btnFIFO.Enabled = true;
            btnStopFIFO.Enabled = false;
            btnLRU.Enabled = true;
            btnStopLRU.Enabled = false;
            btnOPT.Enabled = true;
            btnStopOPT.Enabled = false;

            btnLFU.Enabled = true;
            btnStopLFU.Enabled = false;

            btnALL.Enabled = true;
            btnStopALL.Enabled = false;

            chart1.Series[0].Points.Clear();
            chart2.Series[0].Points.Clear();

        }
        private void image_null(object sender, EventArgs e)
        {
            pictureBoxFIFO.Image = null;
            pictureBoxLRU.Image = null;
            pictureBoxOPT.Image = null;
            pictureBoxLFU.Image = null;
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void chart2_Click(object sender, EventArgs e)
        {

        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            string currPath = Application.StartupPath;
            string subPath = currPath + "/实验结果/";
            if (false == Directory.Exists(subPath))
            {
                Directory.CreateDirectory(subPath);
            }
            string nowtime = DateTime.Now.ToString();
            string temptime = nowtime.Replace(" ", "");
            temptime = temptime.Replace("/", "");
            temptime = temptime.Replace(":", "");
            string temp = subPath + "实验结果" + temptime;//每次实验结果文件夹
            if (false == Directory.Exists(temp))
            {
                Directory.CreateDirectory(temp);
            }
            string path1 = temp + "/FIFO" + ".bmp";
            string path2 = temp + "/LRU" + ".bmp";
            string path3 = temp + "/OPT" + ".bmp";
            string path4 = temp + "/LFU" + ".bmp";
            bp1.Save(path1, System.Drawing.Imaging.ImageFormat.Bmp);
            bp2.Save(path2, System.Drawing.Imaging.ImageFormat.Bmp);
            bp3.Save(path3, System.Drawing.Imaging.ImageFormat.Bmp);
            bp4.Save(path4, System.Drawing.Imaging.ImageFormat.Bmp);
            MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            btnsave.Enabled = false;
        }

        private void groupBox4_Enter(object sender, EventArgs e)
        {

        }
    }
}
